{% extends "admin/change_form.html" %}
{% load staticfiles %}
{% block extrahead %}{{ block.super }}
    <link href="{% static 'admin/css/client.css' %}" type="text/css" rel="stylesheet"/>
    <script src="{% static 'admin/js/agreement_filter_inline.js' %}" type="text/javascript"></script>
    <script src="{% static 'admin/js/popup_fix.js' %}" type="text/javascript"></script>
    <script type="text/javascript" charset="utf-8">
        const _CLIENT_ = "{{ client_id }}";
        (function ($) {
            $(document).ready(function () {
                var oldDismiss = dismissAddAnotherPopup;
                dismissAddAnotherPopup = function (win) {
                    oldDismiss.apply(this, arguments);
                    var select = $("#" + windowname_to_id(win.name));
                    if (select.is("[name$=agreement]")) {
                        if (select.is("[name^=operations]"))
                            agreementInlineFilter({group: "operations-group", url: "{% url 'admin:get_client_agreements' 0 %}", client: _CLIENT_});
                        else
                            agreementInlineFilter({group: "services-group", url: "{% url 'admin:get_client_agreements' 0 %}", client: _CLIENT_});
                    }
                };
                initTabs();
                agreementInlineFilter({group: "operations-group", url: "{% url 'admin:get_client_agreements' 0 %}", client: _CLIENT_});
                agreementInlineFilter({group: "services-group", url: "{% url 'admin:get_client_agreements' 0 %}", client: _CLIENT_});
            });

            function initTabs() {
                var tabsContainer = $("#client_form > div");
                var tabsMenu = $("<ul></ul>").
                        append("<li><a href='#client-main'>Основное</a></li>").
                        append("<li id='individual-tab-link'><a href='#client-properties-individual'>Реквизиты</a></li>").
                        append("<li id='legal-tab-link'><a href='#client-properties-legal'>Реквизиты</a></li>");
                $(".client-main").attr("id", "client-main");
                $(".client-properties-individual").attr("id", "client-properties-individual");
                $(".client-properties-legal").attr("id", "client-properties-legal");

                tabsContainer.prepend(tabsMenu);

                tabsContainer.tabs({ activate: function(event ,ui){
                    $("textarea[id=id_address]").attr('name','_address');
                    $("#id_address", ui.newPanel).attr("name","address");
                } });
                $("fieldset.grp-module h2.grp-collapse-handler").remove();
                $("#id_type").change(function () {
                    $("#individual-tab-link").toggle($(this).val() != "L");
                    $("#legal-tab-link").toggle($(this).val() == "L");
                });
                $("#id_type").change();
            }
        })(grp.jQuery);
    </script>
    {% include 'services/admin_head.html' %}
{% endblock %}
